<?php

declare(strict_types=1);

use think\migration\Migrator;

class CreateFinanceTables extends Migrator
{
    public function change(): void
    {
        $this->createCommissionRuleTable();
        $this->createCommissionLedgerTable();
        $this->createSellerStatementTable();
        $this->createSellerStatementItemTable();
        $this->createInvoiceRequestTable();
        $this->createPayoutTable();
        $this->createPlatformBalanceTable();
        $this->createPlatformBalanceLogTable();
    }

    private function createCommissionRuleTable(): void
    {
        $this->table('commission_rule', [
            'comment' => '佣金规则',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('name', 'string', [
                'limit' => 120,
                'comment' => '规则名称',
            ])
            ->addColumn('scope', 'string', [
                'limit' => 30,
                'default' => 'platform',
                'comment' => '作用范围 platform/category/organization',
            ])
            ->addColumn('category_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '类目ID',
            ])
            ->addColumn('org_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '组织ID',
            ])
            ->addColumn('platform_rate', 'decimal', [
                'precision' => 5,
                'scale' => 2,
                'default' => 0,
                'comment' => '平台抽成比例(%)',
            ])
            ->addColumn('inviter_rate', 'decimal', [
                'precision' => 5,
                'scale' => 2,
                'default' => 0,
                'comment' => '邀请人比例(%)',
            ])
            ->addColumn('service_rate', 'decimal', [
                'precision' => 5,
                'scale' => 2,
                'default' => 0,
                'comment' => '客服比例(%)',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态',
            ])
            ->addColumn('effective_at', 'timestamp', [
                'null' => true,
                'comment' => '生效时间',
            ])
            ->addColumn('expired_at', 'timestamp', [
                'null' => true,
                'comment' => '失效时间',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['scope'])
            ->addIndex(['category_id'])
            ->addIndex(['org_id'])
            ->addIndex(['status'])
            ->create();
    }

    private function createCommissionLedgerTable(): void
    {
        $this->table('commission_ledger', [
            'comment' => '佣金台账',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('order_id', 'integer', [
                'signed' => false,
                'comment' => '订单ID',
            ])
            ->addColumn('order_item_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '订单项ID',
            ])
            ->addColumn('rule_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '佣金规则ID',
            ])
            ->addColumn('beneficiary_type', 'string', [
                'limit' => 30,
                'default' => 'platform',
                'comment' => '受益方类型 platform/inviter/service',
            ])
            ->addColumn('beneficiary_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '受益方用户ID',
            ])
            ->addColumn('amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '佣金金额',
            ])
            ->addColumn('currency', 'string', [
                'limit' => 10,
                'default' => 'CNY',
                'comment' => '币种',
            ])
            ->addColumn('rate', 'decimal', [
                'precision' => 5,
                'scale' => 2,
                'default' => 0,
                'comment' => '比例(%)',
            ])
            ->addColumn('status', 'string', [
                'limit' => 20,
                'default' => 'pending',
                'comment' => '状态 pending/settled/void',
            ])
            ->addColumn('settled_at', 'timestamp', [
                'null' => true,
                'comment' => '结算时间',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['order_id'])
            ->addIndex(['beneficiary_type'])
            ->addIndex(['beneficiary_id'])
            ->addIndex(['status'])
            ->create();
    }

    private function createSellerStatementTable(): void
    {
        $this->table('seller_statement', [
            'comment' => '卖家结算单',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('statement_no', 'string', [
                'limit' => 60,
                'comment' => '结算单号',
            ])
            ->addColumn('seller_org_id', 'integer', [
                'signed' => false,
                'comment' => '卖家组织ID',
            ])
            ->addColumn('period_start', 'date', [
                'comment' => '周期开始',
            ])
            ->addColumn('period_end', 'date', [
                'comment' => '周期结束',
            ])
            ->addColumn('total_order_amount', 'decimal', [
                'precision' => 14,
                'scale' => 2,
                'default' => 0,
                'comment' => '订单总额',
            ])
            ->addColumn('total_refund_amount', 'decimal', [
                'precision' => 14,
                'scale' => 2,
                'default' => 0,
                'comment' => '退款总额',
            ])
            ->addColumn('commission_amount', 'decimal', [
                'precision' => 14,
                'scale' => 2,
                'default' => 0,
                'comment' => '佣金总额',
            ])
            ->addColumn('payable_amount', 'decimal', [
                'precision' => 14,
                'scale' => 2,
                'default' => 0,
                'comment' => '应付金额',
            ])
            ->addColumn('paid_amount', 'decimal', [
                'precision' => 14,
                'scale' => 2,
                'default' => 0,
                'comment' => '已付金额',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 0,
                'comment' => '状态 0-草稿 1-确认中 2-待开票 3-待打款 4-已支付',
            ])
            ->addColumn('generated_at', 'timestamp', [
                'null' => true,
                'comment' => '生成时间',
            ])
            ->addColumn('confirmed_at', 'timestamp', [
                'null' => true,
                'comment' => '确认时间',
            ])
            ->addColumn('invoiced_at', 'timestamp', [
                'null' => true,
                'comment' => '开票时间',
            ])
            ->addColumn('paid_at', 'timestamp', [
                'null' => true,
                'comment' => '打款时间',
            ])
            ->addColumn('operator_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '当前处理人（客服/财务）',
            ])
            ->addColumn('inviter_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '邀请人快照',
            ])
            ->addColumn('service_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '客服快照',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['statement_no'], ['unique' => true])
            ->addIndex(['seller_org_id'])
            ->addIndex(['status'])
            ->create();
    }

    private function createSellerStatementItemTable(): void
    {
        $this->table('seller_statement_item', [
            'comment' => '结算单明细',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('statement_id', 'integer', [
                'signed' => false,
                'comment' => '结算单ID',
            ])
            ->addColumn('order_id', 'integer', [
                'signed' => false,
                'comment' => '订单ID',
            ])
            ->addColumn('order_amount', 'decimal', [
                'precision' => 14,
                'scale' => 2,
                'default' => 0,
                'comment' => '订单金额',
            ])
            ->addColumn('refund_amount', 'decimal', [
                'precision' => 14,
                'scale' => 2,
                'default' => 0,
                'comment' => '退款金额',
            ])
            ->addColumn('commission_amount', 'decimal', [
                'precision' => 14,
                'scale' => 2,
                'default' => 0,
                'comment' => '佣金金额',
            ])
            ->addColumn('net_amount', 'decimal', [
                'precision' => 14,
                'scale' => 2,
                'default' => 0,
                'comment' => '应付净额',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 1-有效 0-无效',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['statement_id'])
            ->addIndex(['order_id'])
            ->create();
    }

    private function createInvoiceRequestTable(): void
    {
        $this->table('invoice_request', [
            'comment' => '发票申请',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('statement_id', 'integer', [
                'signed' => false,
                'comment' => '结算单ID',
            ])
            ->addColumn('invoice_no', 'string', [
                'limit' => 80,
                'default' => '',
                'comment' => '发票号码',
            ])
            ->addColumn('invoice_type', 'string', [
                'limit' => 30,
                'default' => 'special',
                'comment' => '发票类型',
            ])
            ->addColumn('amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '开票金额',
            ])
            ->addColumn('tax_rate', 'decimal', [
                'precision' => 5,
                'scale' => 2,
                'null' => true,
                'comment' => '税率(%)',
            ])
            ->addColumn('tax_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'null' => true,
                'comment' => '税额',
            ])
            ->addColumn('status', 'string', [
                'limit' => 20,
                'default' => 'pending',
                'comment' => '状态 pending/approved/rejected',
            ])
            ->addColumn('submitted_at', 'timestamp', [
                'null' => true,
                'comment' => '提交时间',
            ])
            ->addColumn('reviewed_at', 'timestamp', [
                'null' => true,
                'comment' => '审核时间',
            ])
            ->addColumn('reviewer_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '审核人',
            ])
            ->addColumn('reject_reason', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '驳回原因',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['statement_id'])
            ->addIndex(['status'])
            ->create();
    }

    private function createPayoutTable(): void
    {
        $this->table('payout', [
            'comment' => '打款记录',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('statement_id', 'integer', [
                'signed' => false,
                'comment' => '结算单ID',
            ])
            ->addColumn('payout_no', 'string', [
                'limit' => 60,
                'comment' => '打款编号',
            ])
            ->addColumn('amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '打款金额',
            ])
            ->addColumn('currency', 'string', [
                'limit' => 10,
                'default' => 'CNY',
                'comment' => '币种',
            ])
            ->addColumn('pay_method', 'string', [
                'limit' => 30,
                'default' => 'bank',
                'comment' => '打款方式',
            ])
            ->addColumn('pay_account', 'string', [
                'limit' => 120,
                'default' => '',
                'comment' => '付款账号',
            ])
            ->addColumn('status', 'string', [
                'limit' => 20,
                'default' => 'scheduled',
                'comment' => '状态 scheduled/processing/completed/failed',
            ])
            ->addColumn('scheduled_at', 'timestamp', [
                'null' => true,
                'comment' => '计划打款时间',
            ])
            ->addColumn('paid_at', 'timestamp', [
                'null' => true,
                'comment' => '实际打款时间',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['statement_id'])
            ->addIndex(['payout_no'], ['unique' => true])
            ->addIndex(['status'])
            ->create();
    }

    private function createPlatformBalanceTable(): void
    {
        $this->table('platform_balance', [
            'comment' => '平台资金账户',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('account_code', 'string', [
                'limit' => 60,
                'comment' => '账户编码',
            ])
            ->addColumn('name', 'string', [
                'limit' => 120,
                'comment' => '账户名称',
            ])
            ->addColumn('balance', 'decimal', [
                'precision' => 14,
                'scale' => 2,
                'default' => 0,
                'comment' => '余额',
            ])
            ->addColumn('currency', 'string', [
                'limit' => 10,
                'default' => 'CNY',
                'comment' => '币种',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['account_code'], ['unique' => true])
            ->create();
    }

    private function createPlatformBalanceLogTable(): void
    {
        $this->table('platform_balance_log', [
            'comment' => '平台资金流水',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('balance_id', 'integer', [
                'signed' => false,
                'comment' => '账户ID',
            ])
            ->addColumn('change_no', 'string', [
                'limit' => 60,
                'comment' => '流水号',
            ])
            ->addColumn('change_type', 'string', [
                'limit' => 30,
                'default' => 'adjust',
                'comment' => '变动类型',
            ])
            ->addColumn('amount', 'decimal', [
                'precision' => 14,
                'scale' => 2,
                'comment' => '变动金额',
            ])
            ->addColumn('direction', 'string', [
                'limit' => 10,
                'default' => 'in',
                'comment' => '方向 in/out',
            ])
            ->addColumn('reference_type', 'string', [
                'limit' => 30,
                'default' => '',
                'comment' => '关联类型 order/statement/payout 等',
            ])
            ->addColumn('reference_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '关联ID',
            ])
            ->addColumn('operator_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '操作人',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addColumn('created_at', 'timestamp', [
                'default' => 'CURRENT_TIMESTAMP',
                'comment' => '记录时间',
            ])
            ->addIndex(['balance_id'])
            ->addIndex(['change_no'], ['unique' => true])
            ->addIndex(['reference_type', 'reference_id'])
            ->create();
    }
}
